package com.fzq.server.impl;

import com.fzq.common.DescreaseStockInput;
import com.fzq.common.ProductInfoOutput;
import com.fzq.server.dataObject.ProductInfo;
import com.fzq.server.enums.ProductStatus;
import com.fzq.server.enums.ResultEnum;
import com.fzq.server.exception.ProductException;
import com.fzq.server.repository.ProductInfoRepository;
import com.fzq.server.service.ProductService;
import com.fzq.server.utils.JsonUtil;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.transaction.Transactional;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

@Service
public class ProductServiceImpl implements ProductService {
    @Autowired
    private ProductInfoRepository productInfoRepository;

    @Autowired
    private AmqpTemplate amqpTemplate;

    @Override
    public List<ProductInfo> findUpAll() {
        return productInfoRepository.findByProductStatus(ProductStatus.up.getCode());
    }

    @Override
    public List<ProductInfoOutput> findList(List<String> productIdList) {
        return productInfoRepository.findByProductIdIn(productIdList).stream()
                .map(e -> {
                    ProductInfoOutput output = new ProductInfoOutput();
                    BeanUtils.copyProperties(e, output);
                    return output;
                }).collect(Collectors.toList());
    }

    @Override
    public void descreaseStock(List<DescreaseStockInput> descreaseStockInputList) {
        List<ProductInfo> productInfoList = descreaseStockProcess(descreaseStockInputList);
        List<ProductInfoOutput> productInfoOutputList = productInfoList.stream()
                .map(e -> {
                    ProductInfoOutput productInfoOutput = new ProductInfoOutput();
                    BeanUtils.copyProperties(e, productInfoOutput);
                    return productInfoOutput;
                }).collect(Collectors.toList());
        amqpTemplate.convertAndSend("productInfo_fzq", JsonUtil.toJson(productInfoOutputList));
    }

    @Transactional
    public List<ProductInfo> descreaseStockProcess(List<DescreaseStockInput> descreaseStockInputList) {
        List<ProductInfo> productInfoList = new ArrayList<>();
        for (DescreaseStockInput descreaseStockInput : descreaseStockInputList) {
            Optional<ProductInfo> productInfoOptional = productInfoRepository.findById(descreaseStockInput.getProductId());

            if (!productInfoOptional.isPresent()) {
                throw new ProductException(ResultEnum.PRODUCT_NOT_EXIST);
            }
            ProductInfo productInfo = productInfoOptional.get();
            Integer result = productInfo.getProductStock() - descreaseStockInput.getProductQuantity();
            if (result < 0) {
                throw new ProductException(ResultEnum.PRODUCT_STOCK_ERROR);
            }
            productInfo.setProductStock(result);
            productInfoRepository.save(productInfo);
            productInfoList.add(productInfo);
        }
        return productInfoList;

    }
}
